using KenEgozi.Com.Domain.Aggregates.Common;
using KenEgozi.Com.Domain.Repositories;
using KenEgozi.Com.Weblog.Application;
using Rhino.Commons;

namespace KenEgozi.Com.Weblog.Services
{
	public class SecurityService : ISecurityService
	{
		readonly ICachedItemsService cachedItemsService;

		public SecurityService(ICachedItemsService cachedItemsService)
		{
			this.cachedItemsService = cachedItemsService;
		}

		public bool IsAuthenticated(string username, string password)
		{
			if (username != Preferences.Username)
				return false;

			string hashedPassword = Hashing.HashData(password + "SALTLAKECITY");
			if (hashedPassword != cachedItemsService.AdminPassword)
				return false;

			return true;
		}
		public void SetAdminPassword(string newPassword)
		{
			string hashedPassword = Hashing.HashData(newPassword + "SALTLAKECITY");
			Password adminPassword = Repository.Passwords.Get("Admin");
			adminPassword.Value = hashedPassword;
			With.Transaction(delegate { Repository.Passwords.Update(adminPassword); });
			cachedItemsService.Invalidate(Keys.Cache.AdminPassword);
		}
	}
}